package com.ridaizu.bean;

import java.io.Serializable;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.google.common.base.Strings;
import com.ridaizu.cache.CacheDataBean;
import com.ridaizu.dao.db.DBManager;
import com.ridaizu.lazy.LazyGoodsDamageLostDataModel;
import com.ridaizu.model.GoodsDamageLost;
import com.ridaizu.model.form.GoodsDamageLostForm;
import com.ridaizu.model.search.GoodsDamageLostSearch;
import com.ridaizu.service.impl.GoodsDamageLostServiceImpl;
import com.ridaizu.util.DateUtil;

@javax.faces.bean.ManagedBean(name = "goodsDamageLostBean")
@SessionScoped
public class GoodsDamageLostBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private LazyDataModel<GoodsDamageLost> lazyModel;

    @ManagedProperty("#{cacheDataBean}")
    private CacheDataBean cacheDataBean;

    @ManagedProperty("#{goodsDamageLostServiceImpl}")
    private GoodsDamageLostServiceImpl goodsDamageLostService;

    @ManagedProperty("#{goodsDamageLostSearch}")
    private GoodsDamageLostSearch goodsDamageLostSearch;

    @ManagedProperty("#{goodsDamageLostForm}")
    private GoodsDamageLostForm goodsDamageLostForm;

	public void searchGoodsDamageLostSync() {
		lazyModel = new LazyGoodsDamageLostDataModel(goodsDamageLostSearch);
	}

	public void viewRegisterGoodsDamageLost() throws SQLException {

		setInitValues(null);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 200);

        RequestContext.getCurrentInstance().openDialog("goods-damage-lost", options, null);
    }

	public void viewUpdateGoodsDamageLost(GoodsDamageLost goodsDamageLost) throws SQLException {

		setInitValues(goodsDamageLost);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 220);

        RequestContext.getCurrentInstance().openDialog("goods-damage-lost", options, null);
    }

	private void setInitValues(GoodsDamageLost goodsDamageLost) throws SQLException {
		goodsDamageLostForm.clearAll();
		setValues(goodsDamageLost);
	}

	private void setValues(GoodsDamageLost goodsDamageLost) {
		if (goodsDamageLost == null) {
			setRegisterValues();
		} else {
			setUpdateValues(goodsDamageLost);
		}
	}

	private void setRegisterValues() {
	}

	private void setUpdateValues(GoodsDamageLost goodsDamageLost) {
		goodsDamageLostForm.setId(String.valueOf(goodsDamageLost.getId()));
		goodsDamageLostForm.setTransitId(String.valueOf(goodsDamageLost.getTransitId()));
		goodsDamageLostForm.setGoodsId(String.valueOf(goodsDamageLost.getGoodsId()));
		goodsDamageLostForm.setDamageLostId(String.valueOf(goodsDamageLost.getDamageLostId()));
		goodsDamageLostForm.setCount(String.valueOf(goodsDamageLost.getCount()));
		goodsDamageLostForm.setRemark(goodsDamageLost.getRemark());
		goodsDamageLostForm.setRgtOpt(goodsDamageLost.getRgtOpt());
		goodsDamageLostForm.setRgtDate(new Date(goodsDamageLost.getRgtDate().getTime()));
		goodsDamageLostForm.setUpdOpt(goodsDamageLost.getUpdOpt());
		goodsDamageLostForm.setUpdDate(new Date(goodsDamageLost.getUpdDate().getTime()));
	}

	public void registerGoodsDamageLost() {
		GoodsDamageLost goodsDamageLost = getGoodsDamageLost();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = goodsDamageLostService.insertModel(goodsDamageLost);
			DBManager.commitTransaction();

		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
    }

	public void updateGoodsDamageLost() {

		GoodsDamageLost goodsDamageLost = getGoodsDamageLost();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = goodsDamageLostService.updateModel(goodsDamageLost);
			DBManager.commitTransaction();

		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
	}

	public void deleteGoodsDamageLost() {

		GoodsDamageLost selectedGoodsDamageLost = goodsDamageLostForm.getSelectedGoodsDamageLost() ;

		if (selectedGoodsDamageLost == null || selectedGoodsDamageLost.getId() < 1) {
	        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", "请选择要删除的库存信息。");
	        FacesContext.getCurrentInstance().addMessage(null, message);

	        return ;
		}

		boolean result = false;
		try {
			DBManager.beginTransaction();
			result = goodsDamageLostService.deleteModel(selectedGoodsDamageLost.getId());
			DBManager.commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		String resultStr = null;
		if (result) {
			resultStr = String.format("库存[%s]删除成功。", selectedGoodsDamageLost.getId());
		} else {
			resultStr = String.format("库存[%s]删除失败。", selectedGoodsDamageLost.getId());
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "GoodsDamageLost delete result", resultStr);
        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void registerGoodsDamageLostResult(SelectEvent event) {
		boolean result = (Boolean) event.getObject();

		handleResult(result, "登录");
	}

	public void updateGoodsDamageLostResult(SelectEvent event) {

		boolean result = (Boolean) event.getObject();

		handleResult(result, "更新");
	}

	private void handleResult(boolean result, String optStr) {

		String resultStr = null;
		if (result) {
			resultStr = String.format("库存信息%s成功。", optStr);
		} else {
			resultStr = String.format("库存信息%s失败。", optStr);;
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", resultStr);

        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	private GoodsDamageLost getGoodsDamageLost() {

		GoodsDamageLost goodsDamageLost = new GoodsDamageLost();

		if (!Strings.isNullOrEmpty(goodsDamageLostForm.getId())) {
			goodsDamageLost.setId(Integer.valueOf(goodsDamageLostForm.getId()));
		}

		if (!Strings.isNullOrEmpty(goodsDamageLostForm.getTransitId())) {
			goodsDamageLost.setTransitId(Integer.valueOf(goodsDamageLostForm.getTransitId()));
		}

		if (!Strings.isNullOrEmpty(goodsDamageLostForm.getGoodsId())) {
			goodsDamageLost.setGoodsId(Integer.valueOf(goodsDamageLostForm.getGoodsId()));
		}

		if (!Strings.isNullOrEmpty(goodsDamageLostForm.getDamageLostId())) {
			goodsDamageLost.setDamageLostId(Integer.valueOf(goodsDamageLostForm.getDamageLostId()));
		}

		if (!Strings.isNullOrEmpty(goodsDamageLostForm.getCount())) {
			goodsDamageLost.setCount(Integer.valueOf(goodsDamageLostForm.getCount()));
		}

		goodsDamageLost.setRemark(goodsDamageLostForm.getRemark());

		Timestamp currentDate = DateUtil.getCurrentDateTime();

		goodsDamageLost.setRgtOpt("Admin");
		goodsDamageLost.setRgtDate(currentDate);
		goodsDamageLost.setUpdOpt("Admin");
		goodsDamageLost.setUpdDate(currentDate);

		return goodsDamageLost;
	}

	public LazyDataModel<GoodsDamageLost> getLazyModel() {
		return lazyModel;
	}

	public void setLazyModel(LazyDataModel<GoodsDamageLost> lazyModel) {
		this.lazyModel = lazyModel;
	}

	public GoodsDamageLostServiceImpl getGoodsDamageLostService() {
		return goodsDamageLostService;
	}

	public void setGoodsDamageLostService(GoodsDamageLostServiceImpl goodsDamageLostService) {
		this.goodsDamageLostService = goodsDamageLostService;
	}

	public GoodsDamageLostSearch getGoodsDamageLostSearch() {
		return goodsDamageLostSearch;
	}

	public void setGoodsDamageLostSearch(GoodsDamageLostSearch goodsDamageLostSearch) {
		this.goodsDamageLostSearch = goodsDamageLostSearch;
	}

	public GoodsDamageLostForm getGoodsDamageLostForm() {
		return goodsDamageLostForm;
	}

	public void setGoodsDamageLostForm(GoodsDamageLostForm goodsDamageLostForm) {
		this.goodsDamageLostForm = goodsDamageLostForm;
	}

	public CacheDataBean getCacheDataBean() {
		return cacheDataBean;
	}

	public void setCacheDataBean(CacheDataBean cacheDataBean) {
		this.cacheDataBean = cacheDataBean;
	}

}